/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.reddys.rad.inventory.controller;

import com.reddys.rad.inventory.db.StockDetails;
import com.reddys.rad.inventory.service.StockMovementHistoryService;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.util.Callback;

/**
 * FXML Controller class
 *
 * @author Administrator
 */
public class StockMovementController implements Initializable {

    private  ObservableList data =  FXCollections.observableArrayList();

    @FXML
    TableView tableView;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        populateTableView();
    }    

    private void populateTableView() {
       List list = getStockMovementHistoryService().getStockMovementDetails();
       for(int i=0;i<list.size();i++){
           StockDetails stockDetails = (StockDetails) list.get(i);
           ObservableList dataRow = FXCollections.observableArrayList();
           dataRow.add(stockDetails.getTransactionId());
           dataRow.add(stockDetails.getTransactionType());
           dataRow.add(stockDetails.getTransactionDate()!=null?stockDetails.getTransactionDate().toString():"");
           dataRow.add(stockDetails.getTransactionAmount()!=null?stockDetails.getTransactionAmount().toString():"");
           dataRow.add(stockDetails.getTransactionDetails());
           dataRow.add(stockDetails.getRemarks());
           data.add(dataRow);
       }
       createTableColumns();
       tableView.getItems().addAll(data);
    }
    
    private  StockMovementHistoryService getStockMovementHistoryService(){
        StockMovementHistoryService stockMovementHistoryService = new StockMovementHistoryService();
        return stockMovementHistoryService;
    }

    private void createTableColumns() {
          TableColumn transationID = new TableColumn("Transaction ID");
          transationID.setMinWidth(100);
          createCellValueFactory(transationID,0);
          TableColumn transationType = new TableColumn("Transaction Type");
          transationType.setMinWidth(150);
          createCellValueFactory(transationType,1);
          TableColumn transationDate = new TableColumn("Transaction Date");
          transationDate.setMinWidth(150);
          createCellValueFactory(transationDate,2);
          TableColumn transationAmount = new TableColumn("Transation Amount");
          transationAmount.setMinWidth(150);
          createCellValueFactory(transationAmount,3);
          TableColumn transationDetails = new TableColumn("Transation Details");
          transationDetails.setMinWidth(200);
          createCellValueFactory(transationDetails,4);
          TableColumn remarks = new TableColumn("Remarks");
          remarks.setMinWidth(250);
          createCellValueFactory(remarks,5);
          tableView.getColumns().addAll(transationID,transationType,transationDate,transationAmount,transationDetails,remarks);
    }
    private void createCellValueFactory(TableColumn tableColumn,int columnNo){
        final int columnIndex = columnNo;     
        tableColumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<ObservableList<String>,String>, ObservableValue<String>>(){
             
            @Override
            public ObservableValue<String> call(TableColumn.CellDataFeatures<ObservableList<String>, String> cdf) {
              return new SimpleStringProperty( 
                      cdf.getValue().get(columnIndex)!=null?cdf.getValue().get(columnIndex).toString():"");
            }
              });

    }
    
}
